//
//  APIData.swift
//  lookLikeIweekly
//
//  Created by ZhangXiaoqian on 16/6/5.
//  Copyright © 2016年 Mainzxq. All rights reserved.
//

import Foundation
import UIKit
import Photos

let newsDict = [
    ["title": "美妙的新西兰少女，舞动着妖冶的火炬在广场翩翩起舞",
        "picName": "img1.jpg",
        "picAddress": "http://7xt9pt.com1.z0.glb.clouddn.com/news/image/img1.jpg",
        "newsTime": "2016年 06月 01日",
        "article":"雄伟的新西兰建筑，昏暗的夜色掩映下，少女翩翩起舞，火光闪烁，妖冶动人。",
        "writer": "殷欢欢",
        "photographer":"殷欢欢"],
    ["title": "秘鲁总统候选人",
        "picName": "img2.jpg",
        "picAddress": "http://7xt9pt.com1.z0.glb.clouddn.com/news/image/img2.jpg",
        "newsTime": "2016年 06月 02日",
        "article":"前总统阿尔韦托-滕森之女，滕森庆子举行竞选集会，为竞选造势。",
        "writer": "晓前",
        "photographer":"晓前"],
    ["title": "加沙少年",
        "picName": "img3.jpg",
        "picAddress": "http://7xt9pt.com1.z0.glb.clouddn.com/news/image/img3.jpg",
        "newsTime": "2016年 06月 03日",
        "article":"加沙，在Deir el-Balah难民营的两名巴勒斯坦男孩在驴车上玩耍，背后是涂鸦墙",
        "writer": "晓前",
        "photographer":"晓前"],
]

var emptyDict = ["title": "",
                 "picName": "",
                 "picAddress": "",
                 "newsTime": "",
                 "article":"",
                 "writer": "",
                 "photographer":""]


var titleArray: [(title: String, args: String)] = [ ("推荐", "id=recomm&page=2"),
                   ("娱乐", "id=ent&page=2"),
                   ("社会", "id=society&page=2"),
                   ("财经", "id=finance&page=2"),
                   ("体育", "id=sports&page=2"),
                   ("军事", "id=mil&page=2"),
                   ("健康", "id=health&page=2"),
                   ("科技", "id=tech&page=2"),
                   ("汽车", "id=auto&page=2"),
                   ("旅游", "id=travel&page=2"),
                   ("历史", "id=history&page=2"),
                   ("时尚", "id=fashion&page=2"),
                   ("闺房", "id=couple&page=2"),
                   ("游戏", "id=game&page=2"),
                   ("互联网", "id=internet&page=2"),
                   ("干货", "id=real&page=2"),
                   ("国际", "id=world&page=2"),
                   ("房产", "id=house&page=2")
]



func defineNews() -> [PictureArticleSection] {
    var newsArray = [PictureArticleSection]()
    for item in newsDict {
        let new = PictureArticleSection(picDict: item)
        newsArray.append(new)
    }
    return newsArray
}


func defineNews1(imageArr:[UIImage], assetArray:[PHAsset]) -> [PictureArticleSection] {
    var newsArray = [PictureArticleSection]()
    
    for (index, image) in imageArr.enumerate() {
        var dateTime = (String(assetArray[index].creationDate!) as NSString).substringToIndex(10) as String
        let arr = dateTime.characters.split("-")
        dateTime = String(arr[0]) + "年 " + String(arr[1]) + "月 " + String(arr[2]) + "日"
        emptyDict["title"] = String(index) + "号图片"
        emptyDict["picName"] = String(index)
        emptyDict["picAddress"] = "www.baidu.com"
        emptyDict["newsTime"] = dateTime
        emptyDict["article"] = String(assetArray[index].location)
        emptyDict["writer"] = "晓前"
        emptyDict["photographer"] = "晓前"
        let new = PictureArticleSection(picDict: emptyDict)
        new.albumImage = image
        newsArray.append(new)
        
    }
    return newsArray
}


func workNewsOut(categories: String) -> [PictureArticleSection] {
    var newsArray = [PictureArticleSection]()
    let myData = getDataFromRest(baseUrl + "?" + categories)
    if String(myData["code"]!) == "0" {
        let myRet = myData["data"]!["article"] as! [NSDictionary]
        for (item) in myRet {
            emptyDict["title"] = item["title"] as? String
            emptyDict["picName"] = item["title"] as? String
            if item["img"] as! String == "" {
                continue
            }
            emptyDict["picAddress"] = item["img"] as? String
            emptyDict["newsTime"] = dateToString(NSDate())
            emptyDict["article"] = item["title"] as? String
            emptyDict["writer"] = item["author"] as? String
            emptyDict["photographer"] = "晓前"
            let new = PictureArticleSection(picDict: emptyDict)
            newsArray.append(new)
        }
    }
    if newsArray.count == 0 {
        return workNewsOut(categories)
    }
    return newsArray
}

